package com.bw.Threads.pattern;

import java.util.List;

public class Consumer implements Runnable{

    private List<String> backet;

    public Consumer(List<String> backet) {
        this.backet = backet;
    }

    @Override
    public void run() {
        while (true){
            synchronized (backet){
                while (backet.size() == 0) {
                    try {
                        System.out.println("消费完毕，没有要消费的了");
                        backet.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                backet.remove(0);//消费一个

                System.out.println(Thread.currentThread().getName() + "，包子数量为：" + backet.size());
                backet.notifyAll();//唤醒所有线程
                try {
                    backet.wait();//加上这个后就释放锁了，会切换其他线程
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

             }
        }
    }
}
